using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace XDLPipeline
{
    /// <summary>
    /// Helper functions to parse XML values
    /// </summary>
    class XmlHelper
    {
        /// <summary>
        /// Convert a float string. 
        /// </summary>
        /// <param name="f">float string</param>
        /// <returns>a <see cref="float"/> value.</returns>
        static public float ReadXmlFloat(string f)
        {
            return float.Parse(
                f,
                System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
        }

        /// <summary>
        /// Convert an array of coma (;) separated float values into a vector.
        /// </summary>
        /// <typeparam name="T">Template parameter. Must support a construtor with a T[] parameter.</typeparam>
        /// <param name="rawPosition">values</param>
        /// <returns>A new instance of thge </returns>
        static public T ReadXmlVector<T>(string values)
        {
            string[] strValues = values.Split(';');
            object[] floatValues = new object[strValues.Length];
            Type[] valueTypes = new Type[strValues.Length];
            for (int i = 0; i < floatValues.Length; i++)
            {
                floatValues[i] = ReadXmlFloat(strValues[i]);
                valueTypes[i] = typeof(float);
            }
            return (T)typeof(T).GetConstructor(valueTypes).Invoke(floatValues);
        }

        /// <summary>
        /// Dump an vector in a string representation
        /// </summary>
        /// <param name="v">Vector</param>
        /// <returns>A formated string</returns>
        static public string WriteXmlVector(Vector3 v)
        {
            return string.Format(
                System.Globalization.CultureInfo.InvariantCulture.NumberFormat,
                "{0};{1};{2}",
                v.X,
                v.Y,
                v.Z);
        }
    }
}
